# *****************************************************************
# OVERVIEW ####
# *****************************************************************
# ANES20_CLEAN.R
# Are Rural Attitudes Just Republican?
# CLean ANES Data for Analysis
# Jennifer Lin and Kristin Lunz Trujillo
# Created On: 2023 01 27

# *** The following script generates a cleaned version of
#   the ANES data for the paper ***

# ** Before running this file, ensure that you have ran
#   - Functions.R
#  or components of this code will result in an error. See 
#  00_GENERAL.R for more details **

# *****************************************************************
# PREPARE DATA ####
# *****************************************************************

# The following code generates a cleaned version of the ANES Data
#   for use with the main paper. Key variables that we used are
#   cleaned and renamed in a more intuitive manner for ease of
#   reference.

ANES_Issues <- ANES_2020 %>% 
  mutate(
    # Clean and generate place of residence indicators
    residence       = clean_ANES(V202355),
    resid_Place     = case_when(
      residence == 1 ~ "Rural Area",
      residence == 2 ~ "Small Town",
      residence == 3 ~ "Suburb",
      residence == 4 ~ "City"
    ),
    resid_Place = factor(
      resid_Place,
      levels = c("City", "Suburb", "Small Town", "Rural Area")
    ),
    r_u_identity    = clean_ANES(V202356),
    resid_identity  = case_when(
      r_u_identity == 1 ~ "City Person",
      r_u_identity == 2 ~ "Suburb Person",
      r_u_identity == 3 ~ "Small Town Person",
      r_u_identity == 4 ~ "Country Person",
      r_u_identity == 5 ~ "Something else"
    ),
    resid_identity = factor(
      resid_identity,
      levels = c(
        "City Person", 
        "Suburb Person", 
        "Small Town Person", 
        "Country Person", 
        "Something Else")
    ),
    # Dichotomous variable to reflect Rural residence
    RURAL           = case_when(
      residence %in% c(1, 2) ~ TRUE,
      TRUE ~ FALSE
    ),
    # Generate Partisanship Variable
    pid7            = clean_ANES(V201231x),
    PARTY           = case_when(
      pid7 %in% c(1:3) ~ "Democrat",
      pid7  ==    4    ~ "Independent",
      pid7 %in% c(5:7) ~ "Republican",
      TRUE ~ NA_character_
    ),
    # Generate Place of Residence and Party Combination Variable
    Rural_Party = case_when(
      PARTY == "Democrat" & residence %in% c(1, 2) ~ 
        "Democrat-Rural",
      PARTY == "Democrat" & residence %in% c(3, 4) ~ 
        "Democrat-Urban",
      PARTY == "Republican" & residence %in% c(1, 2) ~ 
        "Republican-Rural",
      PARTY == "Republican" & residence %in% c(3, 4) ~ 
        "Republican-Urban"
    ),
    # Generate more nuanced place and party variable
    Place_Party = case_when(
      resid_Place == "City" & PARTY == "Democrat" ~ 
        "Democrat - City",
      resid_Place == "Suburb" & PARTY == "Democrat" ~ 
        "Democrat - Suburb",
      resid_Place == "Small Town" & PARTY == "Democrat" ~ 
        "Democrat - Small Town",
      resid_Place == "Rural Area" & PARTY == "Democrat" ~ 
        "Democrat - Rural Area",
      resid_Place == "City" & PARTY == "Republican" ~ 
        "Republican - City",
      resid_Place == "Suburb" & PARTY == "Republican" ~ 
        "Republican - Suburb",
      resid_Place == "Small Town" & PARTY == "Republican" ~ 
        "Republican - Small Town",
      resid_Place == "Rural Area" & PARTY == "Republican" ~ 
        "Republican - Rural Area"
    ),
    # Controls for Regression
    # Political Ideology
    ideo7           = clean_ANES(V201200),
    # Gender -- Indicator for Female
    gender          = clean_ANES(V201600),
    FEMALE          = case_when(
      gender == 2 ~ TRUE,
      TRUE ~ FALSE
    ),
    # Education
    educ            = clean_ANES(V201511x),
    # Race -- Indicator for Racial Minority
    race            = clean_ANES(V201549x),
    MINORITY        = case_when(
      race == 1 ~ FALSE,
      TRUE ~ TRUE
    ),
    # Income
    income          = clean_ANES(V201617x),
    # Age
    age             = clean_ANES(V201507x),
    # Church Attendance
    CHURCH          = clean_ANES(V201453),
    CHURCH          = 5 - CHURCH,
    # VOTE CHOICE in 2020 -- Indicators for Biden/Trump
    BIDEN = case_when(
      V202073 == 1 ~ TRUE,
      TRUE ~ FALSE
    ),
    TRUMP = case_when(
      V202073 == 2 ~ TRUE,
      TRUE ~ FALSE
    ),
    Pres_Vote = case_when(
      V202073 == 1 ~ "Joe Biden",
      V202073 == 2 ~ "Donald Trump"
    ),
    # Political Issue Attitudes
    # Allowing refugees who are fleeing war, persecution, or
    #   natural disasters in other countries to come to live 
    #   in the U.S.
    I_refugees = clean_ANES(V202236x),
    I_refugees = 8 - I_refugees,
    # Providing a path to citizenship for unauthorized 
    #   immigrants who obey the law, pay a fine, and
    #   pass security checks?
    I_citizenship = clean_ANES(V202242x),
    I_citizenship = 8 - I_citizenship,
    # Returning all unauthorized immigrants to their 
    #   native countries?
    I_deport = clean_ANES(V202245x),
    I_deport = 8 - I_deport,
    # Separating the children of detained immigrants, rather than 
    #   keeping them with their parents in adult detention centers?
    I_SPChild = clean_ANES(V202248x),
    I_SPChild = 8 - I_SPChild,
    # The government trying to reduce the difference in incomes 
    #   between the richest and poorest households?
    I_reduceIneq = clean_ANES(V202259x),
    I_reduceIneq = 8 - I_reduceIneq,
    # Affordable Care Act of 2010
    I_ACA = clean_ANES(V202328x),
    I_ACA = 8 - I_ACA,
    # Requiring children to be vaccinated in order to attend 
    #   public schools?
    I_reqVax = clean_ANES(V202331x),
    I_reqVax = 8 - I_reqVax,
    # Increased government regulation on businesses that 
    #   produce a great deal of greenhouse emissions linked 
    #   to climate change?
    I_RegGHG = clean_ANES(V202336x),
    I_RegGHG = 8 - I_RegGHG,
    # Requiring background checks for gun purchases at 
    #   gun shows or other private sales?
    I_bkgCheck = clean_ANES(V202341x),
    I_bkgCheck = 8 - I_bkgCheck,
    # Banning the sale of semi-automatic "assault-style" rifles?
    I_banAR = clean_ANES(V202344x),
    I_banAR = 8 - I_banAR,
    # A mandatory program where the government would buy back 
    #   semi-automatic assault-style rifles from citizens who 
    #   currently own them?
    I_buyBackAR = clean_ANES(V202347x),
    I_buyBackAR = 8 - I_buyBackAR,
    # Federal government should be doing more about 
    #   the opioid drug addiction issue
    I_Opioid = clean_ANES(V202350x),
    I_Opioid = 8 - I_Opioid,
    # The U.S. making free trade agreements with other countries?
    I_FreeTrade = clean_ANES(V202361x),
    I_FreeTrade = 8 - I_FreeTrade,
    # #stablishing a federal program that gives all citizens 
    #   $12,000 per year, provided they meet certain conditions?
    I_UBI = clean_ANES(V202376x),
    I_UBI = 8 - I_UBI,
    # Change in government spending to help people pay for health 
    #   insurance when they can’t pay for it all themselves?
    I_incHCspend = clean_ANES(V202380x),
    I_incHCspend = 8 - I_incHCspend,
    # Allowing transgender people to serve in the United States 
    #   Armed Forces?
    I_TransMil = clean_ANES(V202390x),
    I_TransMil = 8 - I_TransMil,
    # Requiring all people to show a government issued 
    #   photo ID when they vote?
    I_ReqIDVote = clean_ANES(V201359x),
    I_ReqIDVote = 8 - I_ReqIDVote,
    # Allowing convicted felons to vote once they complete 
    #   their sentence?
    I_FelonVote = clean_ANES(V201362x),
    I_FelonVote = 8 - I_FelonVote,
    # Elected officials restricting journalists' access to 
    #   information about government decision-making?
    I_JAccess = clean_ANES(V201375x),
    I_JAccess = 8 - I_JAccess,
    # Requiring employers to offer paid leave to parents of 
    #   new children?
    I_FamilyLeave = clean_ANES(V201405x),
    I_FamilyLeave = 8 - I_FamilyLeave,
    # Some people have proposed that the U.S. Constitution should 
    #   be changed so that the children of unauthorized immigrants 
    #   do not automatically get citizenship if they are born 
    #   in this country.
    I_birthright = clean_ANES(V201420x),
    I_birthright = 8 - I_birthright,
    # Building a wall on the U.S. border with Mexico?
    I_BorderWall = clean_ANES(V201426x),
    I_BorderWall = 8 - I_BorderWall,
    # Racial Resentment
    # Irish, Italian, Jewish and many other minorities overcame
    #   prejudice and worked their way up. Blacks should do 
    #   the same without any special favors.
    rr_work         = clean_ANES(V202300),
    # Generations of slavery and discrimination have created 
    #   conditions that make it difficult for blacks to work their 
    #   way out of the lower class.
    rr_slavery      = clean_ANES(V202301),
    # Over the past few years, blacks have gotten less 
    #   than they deserve.
    rr_less         = clean_ANES(V202302),
    # It's really a matter of some people not trying hard enough; 
    #   if blacks would only try harder they could be just as well 
    #   off as whites.
    rr_harder       = clean_ANES(V202303)
  )

# We utilize survey weights in the data analyses. Here, we use the
#   provided survey weight for the full sample pre-election
#   weight given that most of our variables originate from the
#   pre-election wave.

ANES_Survey <- ANES_Issues %>% 
  srvyr::as_survey(weight = V200010a)

# We can clear the workspace to just the objects that
#   we will use in the analyses.

rm(ANES_2020)
